#include "VerticalPositionerWidget.h"

void RVerticalPositionerWidget::CalculatePresentPositionAndSize()
{
    bChildrenArranged=false;
    Super::CalculatePresentPositionAndSize();

    if(Collapsed.Get())
    {
        PresentSize.Y=0;
    }
}

void RVerticalPositionerWidget::CalculateChildrenPresentPositionAndSize()
{
    if(!bChildrenArranged)
    {
        ArrangeChildren();
    }
}

float RVerticalPositionerWidget::CalculateContentHeight()
{

    if(!bChildrenArranged)
    {
        ArrangeChildren();
    }
    return PresentLength;
}

void RVerticalPositionerWidget::PaintSubWidgets( uint16_t &InZOrder)
{
    if(Collapsed.Get())
    {
        return;
    }

    Super::PaintSubWidgets(InZOrder);
}

void RVerticalPositionerWidget::ArrangeChildren()
{
    if(Collapsed.Get())
    {
        PresentLength=0;
        return;
    }

    bChildrenArranged=true;

    float NextWidgetY=0.f;

    for(auto& Child:SubWidgets)
    {
        Child->Y=NextWidgetY;
        Child->CalculatePresentPositionAndSize();
        NextWidgetY+=Child->PresentSize.Y / Child->GetAccumulatedScale().Y + Spacing;
    }

    PresentLength=NextWidgetY;

    OnChildrenArranged.ExecuteIfBound();
}